using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Storage;
using System;
namespace MicroMsg.Scene
{
	public class NetSceneGetUpdateInfo : NetSceneBaseEx<GetUpdateInfoRequest, GetUpdateInfoResponse, GetUpdateInfoRequest.Builder>
	{
		private const string TAG = "NetSceneGetUpdateInfo";
		private const int MIN_UPDATE_INTERVAL = 86400000;
		public bool doScene(UpdateType updateType)
		{
			UpdateInfo updateInfo = this.getUpdateInfo();
			if (updateInfo == null)
			{
				Log.smethod_5("NetSceneGetUpdateInfo", "get updateinfo from network， no data in local file");
				this.doSceneEx(updateType);
				return true;
			}
			if (updateInfo.currentVersion != ConstantsProtocol.CLIENT_VERSION)
			{
				Log.smethod_5("NetSceneGetUpdateInfo", "get updateinfo from network， client version has been changed");
				this.doSceneEx(updateType);
				return true;
			}
			double nowMilliseconds = Util.getNowMilliseconds();
			if (nowMilliseconds > updateInfo.dblLastGetUpdateInfoTimeStamp + 86400000.0)
			{
				Log.smethod_5("NetSceneGetUpdateInfo", "get updateinfo from network， the interval is above one day");
				this.doSceneEx(updateType);
				return true;
			}
			Log.smethod_5("NetSceneGetUpdateInfo", "get updateinfo from local file, info.PackVersion = " + updateInfo.PackVersion);
			if (updateInfo.Int32_0 != 0)
			{
				EventCenter.postEvent(EventConst.ON_NETSCENE_UPDATE_INFO_ERR, updateInfo.Int32_0, null);
				return true;
			}
			EventCenter.postEvent(EventConst.ON_NETSCENE_UPDATE_INFO_SUCCESS, updateInfo, null);
			return true;
		}
		private bool doSceneEx(UpdateType updateType)
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.UpdateType = (uint)updateType;
			this.mSessionPack.mConnectMode = 2;
			this.mSessionPack.mCmdUri = "/cgi-bin/micromsg-bin/getupdateinfo";
			base.endBuilder();
			return true;
		}
		protected override void onFailed(GetUpdateInfoRequest request, GetUpdateInfoResponse response)
		{
			Log.smethod_1("NetSceneGetUpdateInfo", "send GetUpdateInfoRequest failed,network err");
			EventCenter.postEvent(EventConst.ON_NETSCENE_UPDATE_INFO_ERR, RetConst.MM_ERR_CLIENT, null);
		}
		protected override void onSuccess(GetUpdateInfoRequest request, GetUpdateInfoResponse response)
		{
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			UpdateInfo updateInfo = new UpdateInfo();
			if (ret != RetConst.MM_OK)
			{
				Log.smethod_1("NetSceneGetUpdateInfo", "send GetUpdateInfoRequest failed ret =" + ret);
				updateInfo.PackVersion = 0u;
				updateInfo.PackMd5 = "";
				updateInfo.PackSize = 0u;
				updateInfo.PackDescription = "";
				updateInfo.dblLastGetUpdateInfoTimeStamp = Util.getNowMilliseconds();
				updateInfo.currentVersion = ConstantsProtocol.CLIENT_VERSION;
				updateInfo.Int32_0 = (int)ret;
				this.saveUpdateInfo(updateInfo);
				EventCenter.postEvent(EventConst.ON_NETSCENE_UPDATE_INFO_ERR, ret, null);
				return;
			}
			updateInfo.PackVersion = response.PackVersion;
			updateInfo.PackMd5 = response.PackMd5;
			updateInfo.PackSize = response.PackSize;
			updateInfo.PackDescription = response.PackDescription;
			updateInfo.dblLastGetUpdateInfoTimeStamp = Util.getNowMilliseconds();
			updateInfo.currentVersion = ConstantsProtocol.CLIENT_VERSION;
			updateInfo.Int32_0 = (int)ret;
			this.saveUpdateInfo(updateInfo);
			EventCenter.postEvent(EventConst.ON_NETSCENE_UPDATE_INFO_SUCCESS, updateInfo, null);
		}
		private void saveUpdateInfo(UpdateInfo info)
		{
			GConfigMgr.write<UpdateInfo>(info);
		}
		private UpdateInfo getUpdateInfo()
		{
			return GConfigMgr.read<UpdateInfo>();
		}
		private void clearUpdateInfo()
		{
			StorageXml.deleteFile<UpdateInfo>(null);
		}
	}
}
